package user;

import net.rim.blackberry.api.mail.Address;
import net.rim.blackberry.api.mail.Folder;
import net.rim.blackberry.api.mail.Message;
import net.rim.blackberry.api.mail.SendFailedException;
import net.rim.blackberry.api.mail.Session;
import net.rim.blackberry.api.mail.Transport;
import sources.SaveableData;

public class EmailSaveStrategy extends SaveStrategy {

	private final String email = "mousewiz@gmail.com";
	private final String name = "Conor Hoehn";
	
	 public void save(SaveableData sd) {
		 try {
			 Address[] address = new Address[1];
			 address[0] = new Address(email, name);
			 Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);
			 Message msg = new Message(folders[0]);
			 msg.addRecipients(Message.RecipientType.TO, address);
			 msg.setSubject(getSubject(sd));
			 msg.setContent(sd.toString());
			 try {
				 Transport.send(msg);
			 }
			 catch(SendFailedException  e) {
				 e.printStackTrace();
			 }
		 } catch (Exception e){
			 e.printStackTrace();
		 }
	 }

	private String getSubject(SaveableData sd) {
		return "RANDOM_DATA:" + sd.sourceName + ":" + sd.poolName + ":" + System.currentTimeMillis();
	}
}
